package miiteen.customsportal.Api;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import miiteen.core.result.ResultMessage;
import miiteen.core.result.ResultMessageTool;
import miiteen.customsportal.film.beans.FilmInfo;
import miiteen.customsportal.film.service.FilmInfoService;
import miiteen.data.beans.PageBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * 视频影视：
 *     兰州风采、兰州影像
 *
 */
@Api(tags = "视频影像")
@RestController
@RequestMapping("/Api/filmInfo")
@CrossOrigin
public class FilmInfoApiController {

    @Autowired
    private FilmInfoService filmInfoService;

    @GetMapping("findByList")
    @ApiOperation("列表页面")
    public ResultMessage<Object> GetList(){


        List<FilmInfo> list=filmInfoService.findListByMap();

        if(list!=null&&list.size()>0){
            return ResultMessageTool.Success("success",list);
        }
        return ResultMessageTool.Error("未找到相关信息");
    }

    @GetMapping("findById")
    @ApiOperation("详情页面，以及上一条、下一条视频影像信息")
    public ResultMessage<Object> GetOne(String id){
        Map<String,Object> restMap=new HashMap<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        if(id!=null&&id!=""){
            Map<String,Object> map=new HashMap<>();
            map.clear();
           // map.put("id",Long.valueOf(id));
            FilmInfo filmInfo=filmInfoService.findById(Long.valueOf(id));
            if(filmInfo!=null){
                restMap.put("filmInfo",filmInfo);
            }else{
                return  ResultMessageTool.Error("未找到该信息！");
            }
            //上一条，根据发表时间，取当前数据发表时间当作条件，小于当前数据时间的属于上一条
            map.clear();
            map.put("fileTypeId",filmInfo.getFileTypeId());//
            map.put("publishTimePrevious",sdf.format(filmInfo.getPublishTime()));
            List<FilmInfo> dataInfoPrevious=filmInfoService.findByMapPreviousNext(map);
            if(dataInfoPrevious!=null&&dataInfoPrevious.size()>0){
                restMap.put("Previous",dataInfoPrevious.get(0));
            }
            //下一条，根据发表时间，取当前数据发表时间当作条件，大于当前数据时间的属于下一条
            map.clear();
            map.put("fileTypeId",filmInfo.getFileTypeId());//
            map.put("publishTimeNext",sdf.format(filmInfo.getPublishTime()));
            List<FilmInfo> dataInfoNext=filmInfoService.findByMapPreviousNext(map);
            if(dataInfoNext!=null&&dataInfoNext.size()>0){
                restMap.put("Next",dataInfoNext.get(0));
            }

            if(restMap!=null&&restMap.size()>0){
                return ResultMessageTool.Success("success",restMap);
            }
            return  ResultMessageTool.Error("未找到该信息！");
        }

        return ResultMessageTool.Error("未接收到参数信息");
    }

    @GetMapping("findByImage")
    @ApiOperation("兰关风采,前台需要传递给后台一个页码,参数名page，类型int；搜索关键字，title，类型String,页码在第一条数据的delstate字段中")
    public ResultMessage<Object> GetListImage(@RequestParam(required = false) Integer page, String title){

        Map<String,Object> map=new HashMap<>();
        Map<String,Object> resultMap=new HashMap<>();
        PageBounds pageBounds = new PageBounds();
        List<FilmInfo> list=new ArrayList<>();
        List<FilmInfo> count=new ArrayList<>();
        map.put("fileTypeId",2002);//兰关风采
        map.put("order","order by a.publishTime desc");
        if(!"".equals(title)){
            map.put("title" ,title);//搜索条件
        }
        if (page> 0) {
            pageBounds.setPageSize(10);
            pageBounds.setPageNum(page);
            list=filmInfoService.findListByMap(map, pageBounds);
           // count=filmInfoService.findListByMap(map);
            count=filmInfoService.getpages(map);
            resultMap.put("totalCount",count.get(0).getId());
        }else{
            map.put("top",3);
            list=filmInfoService.findListByMap(map);
        }

      //  resultMap.put("totalCount",count.get(1).getId());
        resultMap.put("infoList",list);
        if(list!=null){
            return ResultMessageTool.Success("success",resultMap);
        }
        return ResultMessageTool.Error("未找到相关信息");
    }

    @GetMapping("findByVideo")
    @ApiOperation("兰关影像,前台需要传递给后台一个页码,参数名page，类型int；搜索关键字，title，类型String,页码在第一条数据的delstate字段中。")
    public ResultMessage<Object> GetListVideo(@RequestParam(required = false) Integer page, String title){
        Map<String,Object> resultMap=new HashMap<>();
        Map<String,Object> map=new HashMap<>();
        PageBounds pageBounds = new PageBounds();
        List<FilmInfo> list=new ArrayList<>();
        List<FilmInfo> count=new ArrayList<>();
        map.put("fileTypeId",2003);//兰关影像
        map.put("order","order by a.publishTime desc");
        if(!"".equals(title)){
            map.put("title" ,title);//搜索条件
        }
        if (page > 0 ) {
            pageBounds.setPageSize(10);
            pageBounds.setPageNum(page);
           // count=filmInfoService.findListByMap(map);
            count=filmInfoService.getpages(map);
            list=filmInfoService.findListByMap(map, pageBounds);
            resultMap.put("totalCount",count.get(0).getId());
        }else{
            map.put("top",3);
            pageBounds.setPageNum(page);
            list=filmInfoService.findListByMap(map, pageBounds);
        }

        resultMap.put("infoList",list);
        if(list!=null){
            return ResultMessageTool.Success("success",resultMap);
        }
        return ResultMessageTool.Error("未找到相关信息");
    }

}
